package com.errol.batchprocessor.employeeaddition.advisorimpl.employee;

import com.baomidou.mybatisplus.mapper.Condition;
import com.renjia.dao.*;
import com.renjia.entity.CoreEmployee;
import com.renjia.entity.CoreEmployeeCustomer;
import com.renjia.entity.CoreEmployeeCustomerTmp;
import com.renjia.entity.CoreEmployeeImportBatch;
import com.renjia.enums.EmployeeCustomerTmpValidStatusEnum;
import com.renjia.service.CoreEmployeeImportBatchService;
import com.renjia.service.generic.log.LogService;
import com.renjia.service.generic.middleware.batchprocessor.payload.AbstractBatchPayload;
import com.renjia.service.generic.support.customer.CustomerChannelSupportService;
import com.renjia.service.generic.support.employee.BankCardSupportService;
import com.renjia.service.generic.support.employee.EmployeeCustomerSupportService;
import com.renjia.service.generic.support.employee.EmployeeSupportService;
import com.renjia.service.integration.threeelements.ThreeElementsService;
import com.renjia.service.module.employee.AgreementSigningService;
import com.renjia.service.module.employeeaddition.advisorimpl.EmployeeAdvisorTypeEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.common.AbstractEmployeeAdvisorImpl;
import com.renjia.service.module.employeeaddition.advisorimpl.employee.payload.EmployeeListBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.employee.payload.EmployeeListPiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.employee.payload.EmployeeListSinglePayload;
import com.renjia.util.EpayCallingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户列表导入实现
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/02/26
 * @since v5.0.6
 **/
@Service
public class EmployeeListAdvisorImpl extends AbstractEmployeeAdvisorImpl<Object, CoreEmployeeCustomerTmp, EmployeeListBatchPayload, EmployeeListPiecePayload, EmployeeListSinglePayload> {

    protected final CoreEmployeeCustomerTmpMapper coreEmployeeCustomerTmpMapper;

    public EmployeeListAdvisorImpl(CoreEmployeeImportBatchService coreEmployeeImportBatchService,
                                   CoreEmployeeMapper coreEmployeeMapper, CoreEmployeeCustomerMapper coreEmployeeCustomerMapper,
                                   CoreUserBankMapper coreUserBankMapper, CoreAgreementDetailMapper coreAgreementDetailMapper, ThreeElementsService threeElementsService,
                                   EmployeeSupportService employeeSupportService, EmployeeCustomerSupportService employeeCustomerSupportService,
                                   BankCardSupportService bankCardSupportService, CustomerChannelSupportService customerChannelSupportService,
                                   AgreementSigningService agreementSigningService, EpayCallingService epayCallingService, LogService logService,
                                   CoreEmployeeCustomerTmpMapper coreEmployeeCustomerTmpMapper) {
        super(coreEmployeeImportBatchService, coreEmployeeMapper, coreEmployeeCustomerMapper, coreUserBankMapper,  coreAgreementDetailMapper,threeElementsService,
                employeeSupportService, employeeCustomerSupportService, bankCardSupportService, customerChannelSupportService, agreementSigningService, epayCallingService, logService);
        this.coreEmployeeCustomerTmpMapper = coreEmployeeCustomerTmpMapper;
    }

    @Override
    public boolean shallProcess(@NonNull AbstractBatchPayload<CoreEmployeeImportBatch, EmployeeAdvisorTypeEnum, Object, CoreEmployeeCustomerTmp> advisorPayload) {
        return EmployeeAdvisorTypeEnum.EMPLOYEE_LIST == advisorPayload.getAdvisorTypeEnum();
    }

    @Override
    public EmployeeListPiecePayload packPiecePayload(List<EmployeeListSinglePayload> singlePayloadList) {
        return new EmployeeListPiecePayload(singlePayloadList);
    }

    @Override
    public EmployeeListSinglePayload packSinglePayload(CoreEmployeeCustomerTmp singleEntity) {
        return new EmployeeListSinglePayload(singleEntity);
    }

    @Override
    protected String getDocumentTypeAndNumberString(CoreEmployeeCustomerTmp singleEntity) {
        return singleEntity.getDocumentType() + "-" + singleEntity.getDocumentNumber();
    }

    @Override
    protected boolean isSingleEntityProcessed(EmployeeListSinglePayload singlePayload) {
        return EmployeeCustomerTmpValidStatusEnum.SUCCESS.getCode().equals(singlePayload.getSingleEntity().getValidStatus())
                || EmployeeCustomerTmpValidStatusEnum.FAILED.getCode().equals(singlePayload.getSingleEntity().getValidStatus());
    }

    @Override
    protected boolean isMobileNecessary() {
        return false;
    }

    @Override
    protected void updateValidateSuccess(EmployeeListSinglePayload singlePayload, EmployeeListPiecePayload piecePayload, EmployeeListBatchPayload batchPayload) {
        // 导入成功，更新状态
        singlePayload.getSingleEntity().setValidStatus(EmployeeCustomerTmpValidStatusEnum.SUCCESS.getCode());
        singlePayload.getSingleEntity().setValidResult("");
        this.coreEmployeeCustomerTmpMapper.updateValidateResult(singlePayload.getSingleEntity().getId(), EmployeeCustomerTmpValidStatusEnum.SUCCESS.getCode(), "");
    }

    @Override
    protected void updateValidateFailed(EmployeeListSinglePayload singlePayload, EmployeeListPiecePayload piecePayload, EmployeeListBatchPayload batchPayload, String errMsg) {
        // 导入失败，更新状态
        singlePayload.getSingleEntity().setValidStatus(EmployeeCustomerTmpValidStatusEnum.FAILED.getCode());
        singlePayload.getSingleEntity().setValidResult(errMsg);
        this.coreEmployeeCustomerTmpMapper.updateValidateResult(singlePayload.getSingleEntity().getId(), EmployeeCustomerTmpValidStatusEnum.FAILED.getCode(), errMsg);
    }

    @Override
    protected CoreEmployee newEmployee(EmployeeListSinglePayload singlePayload, EmployeeListBatchPayload batchPayload) {
        CoreEmployee coreEmployee = super.newEmployee(singlePayload, batchPayload);
        coreEmployee.setRemark0(singlePayload.getSingleEntity().getAddress());
        coreEmployee.setRemark(singlePayload.getSingleEntity().getRemark());
        return coreEmployee;
    }

    @Override
    protected boolean renewEmployee(EmployeeListSinglePayload singlePayload, EmployeeListBatchPayload batchPayload) {
        singlePayload.getCoreEmployee().setRemark0(singlePayload.getSingleEntity().getAddress());
        singlePayload.getCoreEmployee().setRemark(singlePayload.getSingleEntity().getRemark());
        return true;
    }

    @Override
    protected boolean additionalUpdateEmployee(EmployeeListSinglePayload singlePayload, EmployeeListBatchPayload batchPayload) {
        if (!StringUtils.equals(singlePayload.getCoreEmployee().getRemark0(), singlePayload.getSingleEntity().getAddress())
                || !StringUtils.equals(singlePayload.getCoreEmployee().getRemark(), singlePayload.getSingleEntity().getRemark())) {
            CoreEmployee update = new CoreEmployee();
            update.setRemark0(singlePayload.getSingleEntity().getAddress());
            update.setRemark(singlePayload.getSingleEntity().getRemark());
            coreEmployeeMapper.update(update, Condition.<CoreEmployee>wrapper().eq("id", singlePayload.getCoreEmployee().getId()));
            return renewEmployee(singlePayload, batchPayload);
        }
        return true;
    }

    @Override
    protected CoreEmployeeCustomer newEmployeeCustomer(EmployeeListSinglePayload singlePayload, EmployeeListBatchPayload batchPayload) {
        CoreEmployeeCustomer coreEmployeeCustomer = super.newEmployeeCustomer(singlePayload, batchPayload);
        coreEmployeeCustomer.setRemark(singlePayload.getSingleEntity().getRemark());
        return coreEmployeeCustomer;
    }

    @Override
    protected boolean renewEmployeeCustomer(EmployeeListSinglePayload singlePayload, EmployeeListBatchPayload batchPayload) {
        singlePayload.getCoreEmployeeCustomer().setRemark(singlePayload.getSingleEntity().getRemark());
        return true;
    }

    @Override
    protected boolean additionalUpdateEmployeeCustomer(EmployeeListSinglePayload singlePayload, EmployeeListBatchPayload batchPayload) {
        if (!StringUtils.equals(singlePayload.getCoreEmployeeCustomer().getRemark(), singlePayload.getSingleEntity().getRemark())) {
            CoreEmployeeCustomer update = new CoreEmployeeCustomer();
            update.setRemark(singlePayload.getSingleEntity().getRemark());
            coreEmployeeCustomerMapper.update(update, Condition.<CoreEmployeeCustomer>wrapper().eq("id", singlePayload.getCoreEmployeeCustomer().getId()));
            return renewEmployeeCustomer(singlePayload, batchPayload);
        }
        return true;
    }
}
